package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.Finance;
import cn.itcast.domain.cargo.FinanceExample;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.service.cargo.FinanceService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.BeanMapUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/finance")
public class FinanceController extends BaseController {
    @Reference
    private FinanceService financeService;
    @Reference
    private InvoiceService invoiceService;

   @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5") int pageSize){
       FinanceExample financeExample = new FinanceExample();
       PageInfo pageInfo = financeService.findAll(financeExample, pageNum, pageSize);
       request.setAttribute("pageInfo",pageInfo);
       return "cargo/finance/finance-list";
   }
    @RequestMapping(value = "/toAdd")
    public String toAdd(){

        List<Invoice> invoices = invoiceService.findByState(1);

        request.setAttribute("invoices", invoices);

        return "cargo/finance/finance-add";

    }

    @RequestMapping("/edit")
    public String edit(Finance finance, String invoiceId) {
        if (StringUtils.isEmpty(finance.getFinanceId())) {

            finance.setFinanceId(invoiceId);
            financeService.save(finance);

            Invoice invoice = new Invoice();
            invoice.setInvoiceId(invoiceId);
            invoice.setState(2);
            invoiceService.update(invoice);

        } else {

            financeService.update(finance);

        }

        return "redirect:/cargo/finance/list.do";

    }


    @RequestMapping("/toUpdate")
    public String toUpdate(String financeId) {

        Finance export = financeService.findById(financeId);

        request.setAttribute("finance", export);

        return "cargo/finance/finance-update";

    }
    //导出pdf报表
    @RequestMapping("/toImport")
    public void toImport(String financeId) throws Exception{

        Finance finance = financeService.findById(financeId);

        String path = session.getServletContext().getRealPath("/")+"/jasper/finance.jasper";

        Map<String, Object> map = BeanMapUtils.beanToMap(finance);

        JasperPrint jasperPrint = JasperFillManager.fillReport(path, map, new JREmptyDataSource());

        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

    }

    /**
     * 导出excel
     *  location.href = "${ctx}/cargo/finance/toExcel.do?financeId=" + id;
     * @param financeId
     * @return
     */
    @RequestMapping("/toExcel")
    @ResponseBody  //下载的时候一定要添加ResponseBody
    public  void printExcel(String financeId) throws IOException {
        Finance finance = financeService.findById(financeId);
        //1. 读取到模板的输入流
        InputStream inputStream = session.getServletContext().getResourceAsStream("/make/xlsprint/tOUTPRODUCT.xlsx");

        //2. 模板的输入流交给工作簿
        Workbook workbook = new XSSFWorkbook(inputStream);

        //3. 获取工作单
        Sheet sheet = workbook.getSheetAt(0);

//        //4. 获取第一行
//        Row row = sheet.getRow(1).getCell(4).setCellValue(finance.getCreateDept());
//        //inputDate=2015-01   2015-12
//        String title = inputDate.replaceAll("-0", "年").replaceAll("-", "年") + "月份出货表";
//        Cell cell = row.getCell(1);
//        //给单元格设置内容
//        cell.setCellValue(title);

    }


    @RequestMapping("/cancel")
    public String cancel(String financeId) {

        Finance finance = new Finance();
        finance.setFinanceId(financeId);
        finance.setState("0");
        financeService.update(finance);

        return "redirect:/cargo/finance/list.do";

    }

    /**
     * /cargo/finance/toView.do?financeId="+id;
     * 查看报运单
     */
    @RequestMapping("/toView")
    public String toView(String financeId) {
        Finance finance = financeService.findById(financeId);
        request.setAttribute("finance", finance);
        return "cargo/finance/finance-view";

    }

}
